﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using Dictionary.ERP;
using Common;
using Security.ERP;
using Extra.ERP;
using Common.Class;

namespace Dictionary
{
    public class xucRateAdd : xucBaseAdd
    {
        public delegate void SuccessEventHander(object sender, DIC_RATE item);

        private readonly IContainer components = null;

        private CalcEdit calCoefficient;
        private CheckEdit chxUse;
        private GridLookUpEdit glkGroupRate;
        private GridColumn gridColumn1;
        private GridColumn gridColumn2;
        private GridView gridView1;
        private LabelControl labelControl1;
        private LabelControl labelControl2;
        private LabelControl lcDescription;
        private TextEdit txtDescription;

        public xucRateAdd()
        {
            InitializeComponent();
            InitData();
        }

        public event SuccessEventHander Success;

        protected override void Add()
        {
            base.Add();
            base.txtID.Text = new DIC_RATE().NewID();
            base.txtNAME.Focus();
        }

        public void Clear()
        {
            base.txtID.Text = "";
            base.txtNAME.Text = "";
            txtDescription.Text = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void glkGroupRate_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            xfmGroupRateAdd.AddedEventHander hander = null;
            if (e.Button.Kind == ButtonPredefines.Plus)
            {
                var edit = sender as GridLookUpEdit;
                string name = edit.Name;
                if ((name != null) && (name == "glkGroupRate"))
                {
                    var add = new xfmGroupRateAdd(Common.Class.Actions.Add);
                    if (hander == null)
                    {
                        hander = (s, i) => new DIC_GROUP_RATE().AddGridLookupEdit(glkGroupRate);
                    }
                    add.Added += hander;
                    add.ShowDialog();
                }
            }
        }

        protected override void Init()
        {
        }

        private void InitData()
        {
            new DIC_GROUP_RATE().AddGridLookupEdit(glkGroupRate);
            calCoefficient.EditValue = 1;
        }

        private void InitializeComponent()
        {
            var manager = new ComponentResourceManager(typeof (xucRateAdd));
            chxUse = new CheckEdit();
            txtDescription = new TextEdit();
            lcDescription = new LabelControl();
            glkGroupRate = new GridLookUpEdit();
            gridView1 = new GridView();
            gridColumn1 = new GridColumn();
            gridColumn2 = new GridColumn();
            labelControl1 = new LabelControl();
            labelControl2 = new LabelControl();
            calCoefficient = new CalcEdit();
            base.gcInfo.BeginInit();
            base.gcInfo.SuspendLayout();
            base.txtNAME.Properties.BeginInit();
            base.txtID.Properties.BeginInit();
            base.imageCollection2.BeginInit();
            ((ISupportInitialize) base.Err).BeginInit();
            chxUse.Properties.BeginInit();
            txtDescription.Properties.BeginInit();
            glkGroupRate.Properties.BeginInit();
            gridView1.BeginInit();
            calCoefficient.Properties.BeginInit();
            base.SuspendLayout();
            base.gcInfo.Controls.Add(calCoefficient);
            base.gcInfo.Controls.Add(labelControl2);
            base.gcInfo.Controls.Add(labelControl1);
            base.gcInfo.Controls.Add(glkGroupRate);
            base.gcInfo.Controls.Add(chxUse);
            base.gcInfo.Controls.Add(txtDescription);
            base.gcInfo.Controls.Add(lcDescription);
            base.gcInfo.Size = new Size(0x163, 0x97);
            base.gcInfo.Controls.SetChildIndex(base.lblId, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtID, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblName, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtNAME, 0);
            base.gcInfo.Controls.SetChildIndex(lcDescription, 0);
            base.gcInfo.Controls.SetChildIndex(txtDescription, 0);
            base.gcInfo.Controls.SetChildIndex(chxUse, 0);
            base.gcInfo.Controls.SetChildIndex(glkGroupRate, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl1, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl2, 0);
            base.gcInfo.Controls.SetChildIndex(calCoefficient, 0);
            base.txtNAME.Location = new Point(0x51, 50);
            base.txtNAME.Size = new Size(0x109, 20);
            base.lblName.Location = new Point(7, 0x36);
            base.txtID.Location = new Point(0x51, 0x1a);
            base.txtID.Size = new Size(0x91, 20);
            base.lblId.Location = new Point(6, 0x1c);
            base.imageCollection2.ImageStream =
                (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            base.btnSave.Location = new Point(0x3d, 0xa2);
            base.btnCancel.Location = new Point(0xfc, 0xa2);
            base.btnSaveNew.Location = new Point(0x9f, 0xa2);
            chxUse.EditValue = true;
            chxUse.Location = new Point(0x4f, 0x80);
            chxUse.Name = "chxUse";
            chxUse.Properties.Caption = "C\x00f2n quản l\x00fd";
            chxUse.Size = new Size(0x61, 0x13);
            chxUse.TabIndex = 15;
            txtDescription.Location = new Point(0x51, 0x4c);
            txtDescription.Name = "txtDescription";
            txtDescription.Size = new Size(0x109, 20);
            txtDescription.TabIndex = 13;
            lcDescription.Location = new Point(5, 0x4e);
            lcDescription.Name = "lcDescription";
            lcDescription.Size = new Size(0x27, 13);
            lcDescription.TabIndex = 14;
            lcDescription.Text = "Ghi ch\x00fa:";
            glkGroupRate.Location = new Point(0x51, 0x66);
            glkGroupRate.Name = "glkGroupRate";
            glkGroupRate.Properties.Buttons.AddRange(new[]
            {new EditorButton(ButtonPredefines.Combo), new EditorButton(ButtonPredefines.Plus)});
            glkGroupRate.Properties.NullText = "[Chọn nh\x00f3m ti\x00eau ch\x00ed]";
            glkGroupRate.Properties.View = gridView1;
            glkGroupRate.Size = new Size(0x109, 20);
            glkGroupRate.TabIndex = 0x23;
            glkGroupRate.ButtonClick += glkGroupRate_ButtonClick;
            gridView1.Columns.AddRange(new[] {gridColumn1, gridColumn2});
            gridView1.FocusRectStyle = DrawFocusRectStyle.RowFocus;
            gridView1.Name = "gridView1";
            gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
            gridView1.OptionsView.ShowGroupPanel = false;
            gridColumn1.Caption = "M\x00e3 nh\x00f3m";
            gridColumn1.FieldName = "GroupRateCode";
            gridColumn1.Name = "gridColumn1";
            gridColumn1.Visible = true;
            gridColumn1.VisibleIndex = 0;
            gridColumn2.Caption = "T\x00ean nh\x00f3m";
            gridColumn2.FieldName = "GroupRateName";
            gridColumn2.Name = "gridColumn2";
            gridColumn2.Visible = true;
            gridColumn2.VisibleIndex = 1;
            labelControl1.Location = new Point(7, 0x69);
            labelControl1.Name = "labelControl1";
            labelControl1.Size = new Size(0x44, 13);
            labelControl1.TabIndex = 0x24;
            labelControl1.Text = "Nh\x00f3m ti\x00eau ch\x00ed:";
            labelControl2.Location = new Point(0xea, 0x1d);
            labelControl2.Name = "labelControl2";
            labelControl2.Size = new Size(0x1f, 13);
            labelControl2.TabIndex = 0x25;
            labelControl2.Text = "Hệ số:";
            calCoefficient.Location = new Point(0x10d, 0x1a);
            calCoefficient.Name = "calCoefficient";
            calCoefficient.Properties.Buttons.AddRange(new[] {new EditorButton(ButtonPredefines.Combo)});
            calCoefficient.Properties.Mask.UseMaskAsDisplayFormat = true;
            calCoefficient.Size = new Size(0x4d, 20);
            calCoefficient.TabIndex = 0x26;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Name = "xucRateAdd";
            base.Size = new Size(0x164, 0xc7);
            base.gcInfo.EndInit();
            base.gcInfo.ResumeLayout(false);
            base.gcInfo.PerformLayout();
            base.txtNAME.Properties.EndInit();
            base.txtID.Properties.EndInit();
            base.imageCollection2.EndInit();
            ((ISupportInitialize) base.Err).EndInit();
            chxUse.Properties.EndInit();
            txtDescription.Properties.EndInit();
            glkGroupRate.Properties.EndInit();
            gridView1.EndInit();
            calCoefficient.Properties.EndInit();
            base.ResumeLayout(false);
        }

        private void RaiseSuccessEventHander(DIC_RATE item)
        {
            if (Success != null)
            {
                Success(this, item);
            }
        }

        public void SetData(DIC_RATE item)
        {
            base.txtID.Text = item.RateCode;
            SYS_LOG.Insert("Ti\x00eau Ch\x00ed Đ\x00e1nh Gi\x00e1", "Xem", base.txtID.Text);
            if (base.m_Status == Actions.Update)
            {
                base.txtID.Properties.ReadOnly = true;
            }
            base.txtNAME.Text = item.RateName;
            calCoefficient.EditValue = item.Coefficient;
            txtDescription.Text = item.Description;
            chxUse.Checked = item.Active;
            new DIC_GROUP_RATE().AddGridLookupEdit(glkGroupRate);
            glkGroupRate.EditValue = item.GroupRateCode;
        }

        protected override void txtID_EditValueChanged(object sender, EventArgs e)
        {
            var control = (TextEdit) sender;
            if (control.ErrorText != string.Empty)
            {
                base.Err.SetError(control, string.Empty);
            }
            if (base.m_Status == Actions.Add)
            {
                var dic_rate = new DIC_RATE();
                if (dic_rate.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            var control = (TextEdit) sender;
            if ((e.KeyCode == Keys.Enter) | (e.KeyCode == Keys.Tab))
            {
                var dic_rate = new DIC_RATE();
                if ((base.m_Status == Actions.Add) && dic_rate.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            var item = new DIC_RATE
            {
                RateCode = base.txtID.Text
            };
            string str = item.Delete();
            if (str == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiJob") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowAdd)
            {
                MyRule.Notify();
                return "";
            }
            if (glkGroupRate.EditValue == null)
            {
                XtraMessageBox.Show("Vui l\x00f2ng chọn 1 nh\x00f3m ti\x00eau ch\x00ed!", "Cảnh B\x00e1o");
                return "";
            }
            SYS_LOG.Insert("Ti\x00eau Ch\x00ed Đ\x00e1nh Gi\x00e1", "Th\x00eam", base.txtID.Text);
            base.SetWaitDialogCaption("Đang lưu dữ liệu...");
            Cursor.Current = Cursors.WaitCursor;
            var item = new DIC_RATE(base.txtID.Text, glkGroupRate.EditValue.ToString(), base.txtNAME.Text,
                double.Parse(calCoefficient.EditValue.ToString()), txtDescription.Text, chxUse.Checked);
            string text = item.Insert();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            Cursor.Current = Cursors.Default;
            this.DoHide();
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return text;
        }

        protected override string uc_Update()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiJob") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowEdit)
            {
                MyRule.Notify();
                return "";
            }
            if (glkGroupRate.EditValue == null)
            {
                XtraMessageBox.Show("Vui l\x00f2ng chọn 1 nh\x00f3m ti\x00eau ch\x00ed!", "Cảnh B\x00e1o");
                return "";
            }
            SYS_LOG.Insert("Ti\x00eau Ch\x00ed Đ\x00e1nh Gi\x00e1", "Cập Nhật", base.txtID.Text);
            base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
            var item = new DIC_RATE(base.txtID.Text, glkGroupRate.EditValue.ToString(), base.txtNAME.Text,
                double.Parse(calCoefficient.EditValue.ToString()), txtDescription.Text, chxUse.Checked);
            string text = item.Update();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.DoHide();
            return text;
        }
    }
}